# +--------------------------
# | User: zq                -
# | Version: python3.7      -
# | Time: 2020-03-16 16:21                
# +--------------------------
import json
import requests
import pickle


def login():
    session = requests.session()
    username = "454168610@qq.com"
    password = "zl4921272"
    url = "https://accounts.douban.com/j/mobile/login/basic"
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
    }

    post_data = {
        "ck": "",
        "name": username,
        "password": password,
        "remember": "true",
        "ticket": ""
    }

    res = session.post(url, data=post_data, headers=headers)
    res_json = json.loads(res.text)
    if res_json['status'] == "success":
        print("登录成功")

        with open("douban.cookie", "wb") as f:
            pickle.dump(res.cookies, f)
    else:
        print("登录失败")

    with open("douban.cookie", "rb") as f:
        cookies = pickle.load(f)
        # 上面已经登录了,下面继续登录首页,看看我们是否登录了
        html = session.get("https://www.douban.com/", headers=headers, cookies=cookies).text
        # 8ll是我的豆瓣昵称,登录之后会显示出来
        if "8ll" in html:
            print("已经登录")
        else:
            print("未登录")


if __name__ == "__main__":
    login()
